package com.fang.uploadFile.controll;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@RestController
public class UploadFileController {


    @Autowired
    private HttpServletRequest request;

    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile[] files) {

        for (MultipartFile file : files) {
            saveFile(file);
        }
        return "chenggong";
    }


    private boolean saveFile(MultipartFile file) {

        try {
            File uploadPic = new java.io.File("/home/fang/Music/" + file.getOriginalFilename());
            if (!uploadPic.exists()) {
                uploadPic.mkdirs();
            }
            file.transferTo(uploadPic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

}
